package com.saxon.Util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUnit {
    private static SqlSessionFactory sqlSessionFactory = null;//获得工厂，产生sqlsession；

    static {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream (resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder ().build (inputStream);
            System.out.println (sqlSessionFactory);
        } catch (IOException e) {
            e.printStackTrace ();
        }

    }


    //获得SQL session对象，就是jdbc中的statement对象，操作数据库
    public static SqlSession getSqlSession () {
        return sqlSessionFactory.openSession ();
    }
}
